#include<iostream>
#include<opencv2/opencv.hpp>

using namespace std;
using namespace cv;

String outputWindow = "output";
Mat src;
Mat dst;
int thresh_value = 127;
int thresh_max = 255;
void threshold_demo(int, void*);
int main()
{
	src = imread("D:/Images/zhang1.jpg");
	if (!src.data)
	{
		printf("can not load image \0XA");
		return -1;
	}
	string inputWindow = "input";
	
	namedWindow(inputWindow, WINDOW_AUTOSIZE);
	namedWindow(outputWindow, WINDOW_AUTOSIZE);

	createTrackbar("Thresh value:", outputWindow, &thresh_value, thresh_max, threshold_demo);
	threshold_demo(0, 0);
	
	cv::imshow(inputWindow, src);
	cv::waitKey(0);
	waitKey(0);
	return 0;
}

void threshold_demo(int, void*)
{
	Mat src_gray;
	cvtColor(src, src_gray, COLOR_RGB2GRAY);
	//threshold(src_gray, dst, thresh_value, 255, THRESH_BINARY);
	//threshold(src_gray, dst, thresh_value, 255, THRESH_BINARY_INV);
	//threshold(src_gray, dst, thresh_value, 255, THRESH_TRUNC);
	double vaue = threshold(src_gray, dst, thresh_value, 255, THRESH_OTSU);
	printf("%f", vaue);

	imshow(outputWindow, dst);
}


